<?php

namespace app\admin\model;

use think\Cache;
use think\Db;
use think\Model;

class QjQuestionModel extends Model {


    public function addQuestion($data)
    {
        $res['success'] = false;
        $res['desc'] = '添加失败';
        $res['errorcode'] = '200022';
        if (empty($data))  return $res;
        $ret = Db::table('qj_question')->insert($data);
        if ($ret) {
            $id = Db::table('qj_question')->getLastInsID();
            $res['success'] = true;
            $res['desc'] = '添加成功';
            $res['data']['id'] = $id;
            $res['errorcode'] = '';
        }
        return $res;
    }

    public function upQuestionInfo($question_id, array $upData)
    {
        Db::table('qj_question')->where(['id' => $question_id])->update($upData);
        $res['success'] = true;
        $res['desc'] = '修改成功';
        $res['errorcode'] = '';
        return $res;
    }

    public function delQuestionInfo($question_id)
    {
        Db::table('qj_question')->where(['id' => $question_id])->delete();
        $res['success'] = true;
        $res['desc'] = '删除成功';
        $res['errorcode'] = '';
        return $res;
    }
    public function getQuestionListByContent($content, $en_content)
    {
        $where = [];
        $whereOr = [];
        if ($content && empty($en_content)) {
            $where['content'] = ['like', '%' . $content . '%'];
        } elseif (empty($content) && $en_content) {
            $where['en_content'] = ['like', '%' . $en_content . '%'];
        } elseif ($content && $en_content) {
            $where['content'] = ['like', '%' . $content . '%'];
            $whereOr['en_content'] = ['like', '%' . $en_content . '%'];
        }

        $ret = Db::table('qj_question')->where($where)->whereOr($whereOr)->order('sort desc, id desc')->select();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data']['list'] = $ret;
        return $res;

    }

    public function getQuestionList($content, $en_content, $parent_id, $pageSize, $page)
    {
        $where = [];
        $whereOr = [];
        if ($content && empty($en_content)) {
            $where[] = ['content', 'like', '%'.$content.'%'];
        } elseif (empty($content) && $en_content) {
            $where[] = ['en_content', 'like', '%'.$en_content.'%'];
        } elseif ($content && $en_content) {
            $where[] = ['content', 'like', '%'.$content.'%'];
            $whereOr[] = ['en_content', 'like', '%'.$en_content.'%'];
        }
        if ($parent_id != -1) {
            $where[] = ['parent_id', '=', $parent_id];
        }
        $offset = ($page-1)*$pageSize;
        if ($offset <= 0) {
            $offset = 0;
        }
        $ret = Db::table('qj_question')->where($where)->whereOr($whereOr)->limit($offset, $pageSize)->order('sort desc, id desc')->select();
        $total = Db::table('qj_question')->where($where)->whereOr($whereOr)->count();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $total_page = 0;
        if ($total) {
            $total_page = ceil($total / $pageSize);
        }
        $res['data']['list'] = $ret;
        $res['data']['total'] = $total;
        $res['data']['total_page'] = $total_page;
        return $res;
    }

    public function getQuestionInfo($question_id)
    {
        $info =  Db::table('qj_question')->where(['id' => $question_id])->find();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data'] = $info;
        return $res;
    }

    public function getQuestionAnswerList($question_id)
    {
        $list =  Db::table('qj_question')->where(['parent_id' => $question_id, 'status' => 1])->select();
        $parentIds = [];
        foreach ($list as $val) {
            $parentIds[] = $val['id'];
        }
        $childList = Db::table('qj_question')->where('parent_id', 'in', $parentIds)->select();
        $type = 0;
        $notEndIds = [];
        if (empty($childList)) {
            $type = 1;
            foreach ($list as &$item) {
                $item['type'] = $type;
            }
        } else {
            foreach ($childList as $v) {
                $notEndIds[] = $v['id'];
            }
            foreach ($list as &$item) {
                if (!in_array($item['id'], $notEndIds)) {
                    $type = 1;
                }
                $item['type'] = $type;
            }
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data']['list'] = $list;
        return $res;
    }


}